/// <reference name="jquery.js"/>
var contentDivId = '#content';

var speechFadeTimeout = 100;

function resetPositionOfSpeech() {
    var right = $(document).width() - $("#profile_picture").offset().left;
    var top = $("#profile_picture").offset().top + $("#profile_picture").height()/4;
    $("#bubble").css({
        right: right+$("#arrow").width(),
        top: top
    });
    $("#arrow").css({
        right: right+12,
        top: top + 10
    });
}

function say(speech, timeout) {
    resetPositionOfSpeech();
    $("#bubble .content").html(speech);
    $("#bubble").fadeIn(speechFadeTimeout);
    $("#arrow").fadeIn(speechFadeTimeout);
    if(timeout > 0) setTimeout("shutup();", 1000*timeout);
}

function shutup() {
    $("#bubble").fadeOut(speechFadeTimeout);
    $("#arrow").fadeOut(speechFadeTimeout);
}

function sayHello() {
    say("Welcome to my website", 5);
}

//ran once on the site startup
function initialize() {
    if ( $.browser.msie ) {
        $("body").addClass("ie");
        if ( $.browser.version == "6.0" ) {
            $("body").addClass("ie6");
        }
    }
    $(".round_me").createRoundedCorner();
    interseptMenuLinks();
    interceptAfterContentLoad();
    shutup();
}

//runs always after ajax content load
function interceptAfterContentLoad(){
    if($("#recent_blog")) $("#recent_blog").createRoundedCorner();
    intersecptAjaxLinkInContent();
    activateSpeech();
    loadRecentBlogFeed();
}

function interseptMenuLinks() {
    $(".ajaxLink", "#navigation").unbind("click").click(
        function() {
            interseptLinkForAjaxContentLoad($(this));
            return false;
        }
    );
}

function interseptLinkForAjaxContentLoad(link) {
    var contentDiv = $(contentDivId);
    href = link.attr("href");

    contentDiv.animate({height: "0%", opacity: 0}, 500, function() {
        contentDiv.animate({height: "100%", opacity: 1}).html("<div class='loading'></div>");
        $.ajax({
          url: href,
          success: function(response) {
            showContents(response);
            //now select the appropiate menu
            $("a", "#navigation").removeClass("selected");
            urlSplits = href.split("/");
            $("a[href$='" + urlSplits[urlSplits.length-1] + "']", "#navigation").addClass("selected");
            
            //Now intercept speech
          }
        });
    });
}

function showContents(response) {
    var contentDiv = $(contentDivId);
    contentDiv.animate({height: "0%", opacity: 0}, 500, function() {
        contentDiv.html(response);
        interceptAfterContentLoad();
    }).animate({height: "100%", opacity: 1}, 500);
}

function intersecptAjaxLinkInContent() {
    $(".ajaxLink", contentDivId).unbind("click").click(
        function() {
            interseptLinkForAjaxContentLoad($(this));
            return false;
        }
    );
}

function activateSpeech() {
    $("#bubble").hide();
    $("#arrow").hide();
    interceptForSpeech();
}

function interceptForSpeech() {
    $("*[speech]").hover(
        function() {
            say($(this).attr("speech"), 0);
        },
        function(f) {
            shutup();
        }
    );
}

function loadRecentBlogFeed() {
    if(!$("#recent_blog")) return;

    var numberOfFeed = 3;
    $.getJSON("http://jitu-blog.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&max-results=" + numberOfFeed + "&callback=?",
        function(data) {
            var recent_blog_contents = '';
            $.each(data.feed.entry, function(i, postentry) {
                var postTitle = postentry.title.$t;
                var postLink = '';
                
                $.each(postentry.link, function(i, link) {
                    postLink = link.href;
                    return;
                });
                var postTags = new Array();
                if(postentry.category) {
                    $.each(postentry.category, function(i, category) {
                        postTags.push(category.term);
                    });
                }
                postTags = postTags.length > 0 ? postTags.join(", ") : "nothing specific";
                var seperator = '';
                if(numberOfFeed != i+1) seperator = "<hr size='1'/>";
                recent_blog_contents += tmpl("blog_link_tmpl", {title:postTitle, link:postLink, seperator:seperator, tags:postTags});
            });
            $("#recent_blog .content").html("<h3>Recent Blogs</h3><br/>" + recent_blog_contents);
            interceptForSpeech();
        });
}